package com.pb.metadata.controller;

import com.pb.metadata.service.DbUserManagementService;
import com.pb.infra.oth.entity.common.Page;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 数据库用户管理
 * @author liaohaoyuan
 * @date 2021/2/25
 */
@Slf4j
@RestController
@RequestMapping("v1/metadata/dbUserManagement")
public class DbUserManagementController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private DbUserManagementService paramColumnService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "param_database_user";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "数据库用户管理";

    /**
     * 查询分页数据
     * @param map 查询参数
     * @param page 页码
     * @return Result 返回数据
     */
    @GetMapping("select")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result select(@RequestParam Map<String, Object> map, Page page) {
        return paramColumnService.select(map, page);
    }

    /**
     * 保存数据库用户
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("insert")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.ADD, menuName = OPERATION_MENU)
    public Result insert(@RequestBody Map<String, Object> paramMap, HttpServletRequest request) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            paramMap.put("createTime",format.format(new Date()));
            Result insert = paramColumnService.insert(paramMap, request);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constant.EXIST, Constant.INSERT_ERROR_MSG);
        }
    }

    /**
     * 修改数据库用户
     *
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("update")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result update(@RequestBody Map<String, Object> paramMap, HttpServletRequest request){
        return paramColumnService.update(paramMap, request);
    }

    /**
     * 查询数据库
     *
     * @return Result 返回数据
     */
    @GetMapping("getDatabaseList")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.QUERY, menuName = OPERATION_MENU)
    public Result getDatabaseList() {
        List<Map<String, Object>> databaseList = paramColumnService.getDatabaseList();
        return Result.ok(databaseList);
    }

    /**
     * 删除数据库用户
     * @param list 请求参数
     * @return Result 返回数据
     */
    @PostMapping("delete")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DEL, menuName = OPERATION_MENU)
    public Result delete(@RequestBody List<String> list){
        paramColumnService.delete(list);
        return Result.OK_EMPTY;
    }

    /**
     * 启用停用数据库用户
     * @param paramMap 请求参数
     * @return Result 返回数据
     */
    @PostMapping("startOrStop")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.MOD, menuName = OPERATION_MENU)
    public Result startOrStop(@RequestBody List<Map<String,Object>> paramMap){
        paramColumnService.startOrStop(paramMap);
        return Result.OK_EMPTY;
    }

}
